﻿using System;
using System.Collections.Generic;
using System.Linq;
using ScrumPilot.TFSHelpers;

namespace ScrumPilot.Domain.Models
{

    public class UserStoryProject : BaseProject
    {
        public List<UserStory> UserStories { get; set; }
        public int TotalTasks { get; set; }
        public int TotalScore { get; set; }
        public decimal TotalEstimatedTime { get; set; }
        public decimal TotalCompletedTime { get; set; }
        public decimal TotalRemainTime { get; set; }
        public int RatioCompleted { get; set; }
        //{
        //    get
        //    {
        //        int value = TotalCompletedTime > TotalEstimatedTime
        //                        ? 100
        //                        : TotalEstimatedTime > 0 ?
        //                        (int)((TotalCompletedTime / TotalEstimatedTime) * 100) :
        //                        0;
        //        return value;
        //    }
        //}
    }

    /// <summary>
    /// Model of user story for list
    /// </summary>
    public class UserStory : WorkItem
    {
        private static readonly int[] Ranks = new[] { 999, 1999, 2999 };
        private static readonly String[] DefRanks = new[] { "Must", "Should", "Could", "N/A" };

        public int StackRank { get; set; }

        public String Rank
        {
            get
            {
                var index = Ranks.TakeWhile(rank => this.StackRank > rank).Count();
                return DefRanks[index];
            }
        }

        public int RatioCompleted
        {
            get
            {
                var value = this.RealRemainTime == 0 ? 0 :
                    (int)((this.CompletedTime / this.RealRemainTime) * 100);
                //? 100
                //: this.EstimatedTime > 0 ?
                //(int)((this.CompletedTime / this.EstimatedTime) * 100) :
                //0;
                return value;
            }
        }

        public decimal RealRemainTime
        {
            get
            {
                var rRet = this.RemainTime;
                switch (this.WIState)
                {
                    case WorkItemState.Todo:
                        rRet = this.EstimatedTime;
                        break;
                    case WorkItemState.InProgress:
                        rRet = Math.Max(rRet, this.EstimatedTime);
                        break;
                    case WorkItemState.Done:
                        rRet = this.CompletedTime;
                        break;
                }
                return rRet;
            }
        }

        public WorkItemState WIState { get; set; }
        public int SubTasks { get; set; }


        public override void CompleteInit(Microsoft.TeamFoundation.WorkItemTracking.Client.WorkItem wie, TFSServices tfs, bool inSameIteration)
        {
            var childList = inSameIteration ? wie.GetTaskChildWorkItemInSameIteration() : wie.GetTaskChildWorkItem();
            if (childList != null && childList.Any())
            {
                this.SubTasks = childList.Count();
                // compute score
                this.Score = childList.Sum(p => p.GetScrumPilotScore());
                this.EstimatedTime = childList.Sum(p => p.GetOriginalEstimate());
                this.CompletedTime = childList.Sum(p => p.GetCompletedWork());
                this.RemainTime = childList.Sum(p => p.GetRemainingWork());
            }
            this.StackRank = wie.GetRank();
            this.WIState = wie.GetState();
            this.State = Helpers.Helpers.WorkItemStateString[(int)this.WIState];
            this.Url = "{0}/" + this.ProjectName + "/_workItems#_a=edit&id=" + this.ID;
        }
    }



}
